from typing import Optional

import PySide6.QtWidgets
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import (QAbstractScrollArea, QSizePolicy, QTreeWidget,
                               QTreeWidgetItem)


class IWTreeWidgetShowDict(QTreeWidget):
    '''
    description:
        一个快速显示字典树形结构的 TreeWidget
    functions:
        - UpdateDict(self, d: 'dict') 更新窗体内部数据
    '''
    def __init__(
        self, 
        parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None:
        ########################################################
        super().__init__(parent=parent)
        # self.setHeaderHidden(True)


    def UpdateDict(self, d: 'dict') -> None:
        '''
        function:
            使用字典来更新本窗体内部的属性结构显示
        args:
            d 'dict' 需要展示的字典，会根据该字典一层层测key值向下展示
        return:
            None
        '''
        self.clear()
        d_ = d
        i_ = QTreeWidgetItem('root')
        stack_d_ = [d_]
        stack_i_ = [i_]
        while (stack_d_):
            dt_ = stack_d_.pop()
            it_ = stack_i_.pop()
            for k_, v_ in dt_.items():
                item_ = QTreeWidgetItem()
                # item_.setSizeHint()
                item_.setText(0, k_)
                it_.addChild(item_)
                stack_d_.append(v_)
                stack_i_.append(item_)
        self.invisibleRootItem().addChild(i_)
